package com.yunchen.server.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yunchen.server.mapper.BaseDaoMapper;
import com.yunchen.server.mapper.RolePermissionsMapper;
import com.yunchen.server.mapper.RolesMapper;
import com.yunchen.server.pojo.RolePermissions;
import com.yunchen.server.pojo.RolePermissionsExample;
import com.yunchen.server.pojo.Roles;
import com.yunchen.server.util.Return;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

@Service
public class RolesService {
    @Autowired
    private BaseDaoMapper baseDaoMapper;
    @Autowired
    private RolesMapper rolesMapper;
    @Autowired
    private RolePermissionsMapper rolePermissionsMapper;
    public Return Add(JSONObject json) {
        Return re = new Return();
        String roleName = json.getString("roleName");
        String roleCode = json.getString("roleCode");
        String description = json.getString("description");
        Integer status = json.getInteger("status");
        JSONArray checkedKeys = json.getJSONArray("checkedKeys");
        Roles roles=new Roles();
        roles.setRoleCode(roleCode);
        roles.setRoleName(roleName);
        roles.setDescription(description);
        roles.setStatus(status);
        rolesMapper.insertSelective(roles);
        Integer roleId = roles.getRoleId();
        for (int i=0; i<checkedKeys.size();i++){
            Integer integer = checkedKeys.getInteger(i);
            RolePermissions rolePermissions=new RolePermissions();
            rolePermissions.setPermissionId(integer);
            rolePermissions.setRoleId(roleId);
            rolePermissions.setAssignedAt(new Date());
            rolePermissionsMapper.insertSelective(rolePermissions);
        }
        re.setCode(200);
        re.setMsg("成功");
        return re;
    }

    public Return Update(JSONObject json) {
        Return re = new Return();
//        rolesMapper.updateByPrimaryKeySelective(roles);

        Integer roleId = json.getInteger("roleId");
        String roleName = json.getString("roleName");
        String roleCode = json.getString("roleCode");
        String description = json.getString("description");
        Integer status = json.getInteger("status");
        JSONArray checkedKeys = json.getJSONArray("checkedKeys");
        Roles roles=new Roles();
        roles.setRoleCode(roleCode);
        roles.setRoleName(roleName);
        roles.setDescription(description);
        roles.setStatus(status);
        roles.setRoleId(roleId);
        rolesMapper.updateByPrimaryKeySelective(roles);
        baseDaoMapper.deleteSql("DELETE from role_permissions where role_id="+roleId+" ");
        for (int i=0; i<checkedKeys.size();i++){
            Integer integer = checkedKeys.getInteger(i);
            RolePermissions rolePermissions=new RolePermissions();
            rolePermissions.setPermissionId(integer);
            rolePermissions.setRoleId(roleId);
            rolePermissions.setAssignedAt(new Date());
            rolePermissionsMapper.insertSelective(rolePermissions);
        }


        re.setCode(200);
        re.setMsg("成功");
        return re;
    }
    public Return Del( Roles roles) {
        Return re = new Return();
        rolesMapper.deleteByPrimaryKey(roles.getRoleId());
        baseDaoMapper.deleteSql("DELETE from role_permissions where role_id="+roles.getRoleId()+" ");
        re.setCode(200);
        re.setMsg("成功");
        return re;
    }

    public Integer Count(JSONObject json) {
        String role_name = json.getString("role_name");
        String tj="";
        if (role_name!=null){
            if (role_name!=""){ tj=tj+"  and  role_name like '%"+role_name+"%'  "; }
        }
        String SQL="SELECT  count(role_id) as mun  from roles where 1=1 "+tj+"   ";
        Integer integer = baseDaoMapper.selectCountSql(SQL);
        return integer;
    }
    public JSONArray List (JSONObject json) {
        Integer page = json.getInteger("page");
        Integer limit = json.getInteger("limit");
        String role_name = json.getString("role_name");
        String tj="";
        if (role_name!=null){
            if (role_name!=""){ tj=tj+"  and  role_name like '%"+role_name+"%'  "; }
        }
        String SQL="SELECT * FROM roles where 1=1  "+tj+"  ORDER BY role_id desc LIMIT "+(page-1)*limit+","+limit+"  ";
        List<LinkedHashMap<String, Object>> linkedHashMaps = baseDaoMapper.selectSql(SQL);
        JSONArray listArray = JSON.parseArray(JSON.toJSONString(linkedHashMaps));
        JSONArray array=new JSONArray();

        for (int i=0; i<listArray.size();i++){
            JSONObject jsonObject = listArray.getJSONObject(i);
            JSONObject js=new JSONObject();
            js.put("role_id",jsonObject.getInteger("role_id"));
            js.put("status",jsonObject.getInteger("status"));
            js.put("role_name",jsonObject.getString("role_name"));
            js.put("role_code",jsonObject.getString("role_code"));
            js.put("description",jsonObject.getString("description"));
            js.put("created_at",jsonObject.getString("created_at"));

            RolePermissionsExample rolePermissionsExample=new RolePermissionsExample();
            rolePermissionsExample.createCriteria().andRoleIdEqualTo(jsonObject.getInteger("role_id"));
            List<RolePermissions> rolePermissions = rolePermissionsMapper.selectByExample(rolePermissionsExample);
            JSONArray jsonArray=new JSONArray();
            for (int k=0; k<rolePermissions.size();k++){
                Integer permissionId = rolePermissions.get(k).getPermissionId();
                jsonArray.add(permissionId);
            }
            js.put("checkedKeys",jsonArray);

            array.add(js);
        }

        return array;
    }


    public JSONArray PullDown () {
        String SQL="SELECT * FROM roles where  status=1  ORDER BY role_id asc   ";
        List<LinkedHashMap<String, Object>> linkedHashMaps = baseDaoMapper.selectSql(SQL);
        JSONArray listArray = JSON.parseArray(JSON.toJSONString(linkedHashMaps));
        return listArray;
    }

}
